package com.cskaoyan.gateway.controller.user;

import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.commons.tool.utils.CookieUtil;
import io.netty.handler.codec.http.HttpResponse;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author hanshuai
 * @version 1.0
 * @description @TODO
 * @date 2021/12/13 9:09
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserLogoutController {

    @RequestMapping("/loginOut")
    public ResponseData userLogout(HttpServletRequest request, HttpServletResponse response){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if ("access_token".equals(cookie.getName())){
                //清空cookie内容并设置过期时间
                cookie.setValue("");
                cookie.setMaxAge(0);
                cookie.setPath("/");
                response.addCookie(cookie);
                String remoteAddr = request.getRemoteAddr();
                log.info("地址"+remoteAddr+" 的用户 已退出登录");
            }
        }
        return new ResponseUtil().setData(null);
    }
}
